<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class User extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function login()
	{
		$this->show(array('header', 'login', 'footer'));
	}

	public function verify()
	{
		$captcha = $this->input->post('captcha');

		if (strtolower($this->session->img_captcha['word']) != strtolower($captcha)) {
			ajax_return(1, '验证码错误');
		}

		$username = $this->input->post('username');
		$password = $this->input->post('password');

		if (!$username) {
			ajax_return(1, '用户名不能为空');
		}

		if (!$password) {
			ajax_return(1, '密码不能为空');
		}

		$user = $this->db->where('username', $username)->get('user')->row_array();

		if (!$user) {
			ajax_return(1, '用户名或密码错误');
		}

		if ($this->pwd->verify($password, $user['password'])) {
			if (is_file($_SERVER['DOCUMENT_ROOT'] . '/../session/ci_session' . $user['session_id'])) {
				unlink($_SERVER['DOCUMENT_ROOT'] . '/../session/ci_session' . $user['session_id']);
			}

			$this->db->where('id', $user['id'])->set('password', $this->pwd->encrypt($password))->set('last_login_time', time())->set('session_id', $this->session->session_id)->update('user');
			$this->db->set('add_time', time())->set('user_id', $user['id'])->set('ip', $this->input->ip_address())->insert('login_log');
			$this->session->user_id = $user['id'];
			$this->session->username = $user['username'];
			ajax_return(0, '登录成功', site_url('safe'));
		}
		else {
			ajax_return(1, '用户名或密码错误');
		}
	}

	public function reg()
	{
		$d['uid'] = intval($this->input->get('uid'));
		$this->show(array('header', 'reg', 'footer'), $d);
	}

	public function insert()
	{
		$captcha = $this->input->post('captcha');

		if (strtolower($this->session->img_captcha['word']) != strtolower($captcha)) {
			ajax_return(1, '验证码错误');
		}

		$uid = intval($this->input->post('uid'));

		if (0 < $uid) {
			$puser = $this->db->where('id', $uid)->count_all_results('user');

			if (!$puser) {
				ajax_return(1, '找不到推荐人');
			}
		}

		$username = $this->input->post('username');
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');

		if (!is_username($username)) {
			ajax_return(1, '用户名为字母开头4 - 15个字符');
		}

		if (!is_email($email)) {
			ajax_return(1, '邮箱格式错误');
		}

		if (!is_password($password)) {
			ajax_return(1, '密码为6-20个字符');
		}

		if (!is_password($repassword)) {
			ajax_return(1, '确认密码错误');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		$exist = $this->db->where('username', $username)->count_all_results('user');

		if ($exist) {
			ajax_return(1, '该用户名已被使用');
		}

		$data['username'] = $username;
		$data['password'] = $this->pwd->encrypt($password);
		$data['add_time'] = time();
		$coins = $this->db->get('coin')->result_array();
		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_user write, t_xnb write, t_intro write');
		$rs = array();
		$rs[] = $this->db->insert('user', $data);
		$user_id = $this->db->insert_id();

		foreach ($coins as $v) {
			$rs[] = $this->db->insert('xnb', array('user_id' => $user_id, 'coin_id' => $v['id']));
		}

		if (0 < $uid) {
			$rs[] = $this->db->set('user_id', $uid)->set('child_user_id', $user_id)->set('add_time', time())->insert('intro');
		}

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			$this->session->user_id = $user_id;
			ajax_return(0, '注册成功', site_url('safe'));
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '注册失败');
		}

		echo json_encode($d);
	}

	public function log_out()
	{
		$this->session->sess_destroy();
		redirect('user/login');
	}

	public function forget()
	{
		$this->show(array('header', 'forget', 'footer'));
	}

	public function forget_setpwd()
	{
		$this->show(array('header', 'forget_setpwd', 'footer'));
	}

	public function forget_check()
	{
		$s_email = (isset($this->session->email) ? $this->session->email : '');
		$s_email_captcha = (isset($this->session->email_captcha) ? $this->session->email_captcha : '');
		if (!$s_email || !$s_email_captcha) {
			ajax_return(1, '信息有误');
		}

		$email = $this->input->post('email');
		$email_captcha = $this->input->post('email_captcha');
		if (!$email || !$email_captcha) {
			ajax_return(1, '信息有误');
		}

		if ($email != $s_email) {
			ajax_return(1, '该用户不存在');
		}

		if ($s_email_captcha != $email_captcha) {
			ajax_return(1, '验证码不正确');
		}

		$user = $this->db->where('email', $s_email)->get('user')->row_array();

		if (!$user) {
			ajax_return(1, '该用户不存在');
		}

		ajax_return(0, '正在跳转', site_url('user/forget_setpwd'));
	}

	public function forget_set()
	{
		$s_email = (isset($this->session->email) ? $this->session->email : '');

		if (!$s_email) {
			ajax_return(1, '信息有误');
		}

		$user = $this->db->where('email', $s_email)->get('user')->row_array();

		if (!$user) {
			ajax_return(1, '该用户不存在');
		}

		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');

		if (!is_password($password)) {
			ajax_return(1, '密码错误');
		}

		if (!is_password($repassword)) {
			ajax_return(1, '确认密码错误');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		$data['password'] = $this->pwd->encrypt($password);
		$rs = $this->db->where('id', $user['id'])->update('user', $data);

		if ($rs) {
			ajax_return(0, '修改成功', site_url('user/login'));
		}
		else {
			ajax_return(1, '修改失败');
		}
	}

	public function reg_success()
	{
		$d['email'] = urldecode($this->uri->segment(3));

		if ($d['email']) {
			$this->show(array('header', 'reg_success', 'footer'), $d);
		}
	}

	public function verify_email()
	{
		$email = urldecode($this->uri->segment(3));
		$code = $this->uri->segment(4);
		$user = $this->db->where('email', $email)->get('user')->row_array();

		if (md5($user['email'] . $user['id']) == $code) {
			$rs = $this->db->where('id', $user['id'])->update('user', array('status' => 1));

			if ($rs) {
				$d['error'] = 0;
			}
			else {
				$d['error'] = 1;
			}
		}
		else {
			$d['error'] = 1;
		}

		$this->show(array('header', 'verify_success', 'footer'), $d);
	}
}

?>
